<?php

namespace Hilaoyu\LaravelExtend\Services\Files\Console;


use Carbon\Carbon;
use Hilaoyu\LaravelExtend\Services\Files\FileService;
use Hilaoyu\LaravelExtend\Supports\Cli;
use Hilaoyu\Utils\UtilCli;
use Hilaoyu\Utils\UtilFile;
use Illuminate\Console\Command;

class ClearTempCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'laravelExtend:filesCleanTemp';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Laravel extend clear temp disk';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        return $this->clearTempDisk();

    }

    protected function clearTempDisk(){

        $temp_file_hold_time = intval(config('file_service.temp_file_hold_time',86400));
        //dump($temp_file_hold_time);
        if($temp_file_hold_time <=0){
            return 0;
        }
        //dump($temp_file_hold_time);
        $last_time = Carbon::now()->unix() - $temp_file_hold_time;

        //清理上传目录
        $tempStorage = FileService::makeStorage(FileService::tempDisk());
        UtilFile::delDirOrFile($tempStorage->path('/'),function ($file) use ($last_time){
            if(is_dir($file)){
                return true;
            }

            if( intval(filemtime($file) <= $last_time)){
                $this->info('delete temp file:'.$file);
                return true;
            }

            return false;

        });


        return 0;
    }
}
